package org.teachertools.web.forms;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.time.DateFormatUtils;
import org.teachertools.common.ValidationUtils;
import org.teachertools.data.Categorie;
import org.teachertools.data.Competence;
import org.teachertools.data.Eleve;
import org.teachertools.data.Matiere;
import org.teachertools.data.Note;
import org.teachertools.web.DateConvertUtils;
import org.teachertools.web.I18nUtils;

public class NoteFormMapping {
    public static Note mappingFormToDTO(NoteForm noteForm, HttpServletRequest request) {
        return mappingFormToDTO(noteForm, new Note(), request);
    }

    public static Note mappingFormToDTO(NoteForm noteForm, Note note, HttpServletRequest request) {

        if (ValidationUtils.isNotEmpty(noteForm.getId())) {
            Long idTypedValue = noteForm.getId();
            note.setId(idTypedValue);
        }
        if (ValidationUtils.isNotEmpty(noteForm.getEleve())) {
            Eleve eleveTypedValue = new Eleve();
            eleveTypedValue.setId(noteForm.getEleve());
            note.setEleve(eleveTypedValue);
        }
        if (ValidationUtils.isNotEmpty(noteForm.getNote())) {
            java.lang.String noteTypedValue = noteForm.getNote();
            note.setNote(noteTypedValue);
        }
        if (ValidationUtils.isNotEmpty(noteForm.getCommentaire())) {
            note.setCommentaire(noteForm.getCommentaire());
        }
        if (ValidationUtils.isNotEmpty(noteForm.getDateNote())) {
            java.util.Date dateNoteTypedValue = DateConvertUtils.parseDate(noteForm.getDateNote(), I18nUtils
                    .getMessage(request, "FORMAT_DATE_COURT"));
            note.setDateNote(dateNoteTypedValue);
        }
        if (ValidationUtils.isNotEmpty(noteForm.getFinDateNote())) {
            java.util.Date dateNoteTypedValue = DateConvertUtils.parseDate(noteForm.getFinDateNote(), I18nUtils
                    .getMessage(request, "FORMAT_DATE_COURT"));
            note.setFinDateNote(dateNoteTypedValue);
        }
        if (ValidationUtils.isNotEmpty(noteForm.getIdCompetence())) {
            Competence competence = new Competence();
            Long id = noteForm.getIdCompetence();
            if (id != 0) {
                competence.setId(id);
            }
            note.setCompetence(competence);
        }
        if (ValidationUtils.isNotEmpty(noteForm.getIdMatiere())) {
            Matiere mat = new Matiere();
            Long id = noteForm.getIdMatiere();
            if (id != 0) {
                mat.setId(id);
            }
            if (note.getCompetence() == null) {
                note.setCompetence(new Competence());
            }
            note.getCompetence().setMatiere(mat);
        }
        if (ValidationUtils.isNotEmpty(noteForm.getIdCategorie())) {
            Categorie cat = new Categorie();
            Long id = noteForm.getIdCategorie();
            if (id != 0) {
                cat.setId(id);
            }
            if (note.getCompetence() == null) {
                note.setCompetence(new Competence());
            }
            if (note.getCompetence().getMatiere() == null) {
                note.getCompetence().setMatiere(new Matiere());
            }
            note.getCompetence().getMatiere().setCategorie(cat);
        }
        return note;
    }

    public static void mappingDTOToForm(Note note, NoteForm noteForm, HttpServletRequest request) {
        noteForm.reset();

        if (ValidationUtils.isNotEmpty(note.getId())) {
            Long idTypedValue = note.getId();
            noteForm.setId(idTypedValue);
        }
        if (ValidationUtils.isNotEmpty(note.getEleve())) {
            noteForm.setEleve(note.getEleve().getId());
        }
        if (ValidationUtils.isNotEmpty(note.getCompetence())) {
            noteForm.setIdCompetence(note.getCompetence().getId());
            if (note.getCompetence().getMatiere() != null) {
                noteForm.setIdMatiere(note.getCompetence().getMatiere().getId());
                if (note.getCompetence().getCategorie() != null) {
                    noteForm.setIdCategorie(note.getCompetence().getCategorie().getId());
                }
            }
        }
        if (ValidationUtils.isNotEmpty(note.getNote())) {
            java.lang.String noteTypedValue = note.getNote();
            noteForm.setNote(noteTypedValue);
        }
        if (ValidationUtils.isNotEmpty(note.getCommentaire())) {
            java.lang.String noteTypedValue = note.getCommentaire();
            noteForm.setCommentaire(noteTypedValue);
        }
        if (ValidationUtils.isNotEmpty(note.getDateNote())) {
            java.lang.String dateNoteTypedValue = DateFormatUtils.format(note.getDateNote(), I18nUtils.getMessage(
                    request, "FORMAT_DATE_COURT"));
            noteForm.setDateNote(dateNoteTypedValue);
        }
    }
}
